<?php

namespace App\Http\Controllers\Admin\Logs;

use App\Http\Controllers\AdminController;
use App\Library\NginxLog;
use Illuminate\Http\Request;

class AccessLogController extends AdminController
{

    public function data(Request $request)
    {
        $logs = NginxLog::init()->get();
        if ($ip = $request->get('ip')) {
            $logs = $logs->where('ip', $ip);
        }
        if ($url = $request->get('url')) {
            $logs = $logs->where('url', $url);
        }
        if ($method = $request->get('method')) {
            $logs = $logs->where('method', $method);
        }
        $startAt = $request->get('start_at');
        $endAt = $request->get('end_at');
        if ($startAt && $endAt) {
            $logs = $logs->whereBetween('time', [$startAt, $endAt]);
        }
        $total = $logs->count();
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $data = $logs->slice(($page-1)*$pageSize, $pageSize);
        return $this->succeed([
            'total' => $total,
            'rows'  => $data->values()
        ]);
    }
}
